<?php

namespace App\Admin\Controllers;

use App\Models\Team;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PlayersController extends AdminController
{

    public function index(Content $content)
    {
        return $content->title('球员')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()->where('level', 1)->orderByDesc('update_time');
            $grid->fixColumns(2, -1);
            $grid->column('user_name', '姓名');
            $grid->column('user_phone', '电话');
            $grid->column('position', '位置');
            $grid->column('evaluate', '教练评价')->display(function ($value){
                return str_limit(stripslashes($value),20);
            });
            $grid->position('位置')->display(function ($pictures) {
                $res =  json_decode($pictures, true);
                if ($res){
                    return json_decode($pictures)[0].'/'.json_decode($pictures)[1] ?? '';
                }else{
                    return '';
                }
            });

            $grid->column('play_img', '证件照')->image(config('alioss.ossServer'), 50, 50);
            $grid->user_img('训练图片')->display(function ($pictures) {
                return json_decode($pictures, true);

            })->image(config('alioss.ossServer'), 50, 50);
            $grid->column('user_video', '训练视频')->display(function ($pictures) {
//                json_decode($pictures)[0].'/'.json_decode($pictures)[1] ?? '';
                $res = json_decode($pictures, true);
                if ($res){
                   static $a = '';
                    $len = (count($res));
                    for ($i=0;$i<$len;$i++){
                        $c = $i+1;
                        $a.= "<span><a target='_blank' href=".config('alioss.ossServer').'/'.json_decode($pictures)[$i]." width='200' preload='auto' controls>视频链接$c</a><span>&nbsp; &nbsp; ";
                    }
                    return $a;
                }else{
                    return '';
                }
            });
            $grid->column('team_id', '所属球队')->display(function ($value) {
                if ($value) {
                    return Team::getTeamById($value);
                } else {
                    return '暂未加入球队';
                }
            });

            $grid->column('is_best', '是否优秀')->switch();
            $grid->column( '解绑操作')->display(function (){
                return "
                    <button class='btn btn-sm btn-custom'  id='data-" . $this->id . "'>解绑球队</button>
                           <script>
                                   $('#data-" . $this->id . "').unbind('click').click(function() {
                      Dcat.confirm('确认解绑球队', null, function () {
                          $.post({
                                    method: 'get',
                                    url: '/admin/Players/" . $this->id . "/change',
                                    success: function (data) {
                                        if (typeof data === 'object') {
                                            if (data.status) {
                                                Dcat.swal.success('解绑成功');
                                                window.location.reload();
                                            } else {
                                                Dcat.swal.error('解绑失败');
                                            }
                                        }
                                    }
                                });
                              });
                             })
                           </script>
                    ";
            });


            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('user_phone', '手机号')->width(3);
                $filter->like('user_name', '真实姓名')->width(3);
                $filter->where('team_id', function ($query) {
                    $query->whereHas('team', function ($query) {
                        $query->where('team_name', 'like', "%{$this->input}%");
                    });
                }, '球队名称')->width(3);
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
            $grid->disableCreateButton();

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->display('user_name', '姓名');
            $form->display('user_phone', '电话');
            $form->text('evaluate', '教练评价');
            $form->file('play_img','证件照')->autoUpload()->uniqueName();
            $form->switch('is_best', '优秀开关');

            $form->multipleFile('user_img', '精彩图集')->saving(function ($paths) {
                // 可以转化为由 , 隔开的字符串格式
                // return implode(',', $paths);
                // 也可以转化为json
                return json_encode($paths);
            })->autoUpload()->uniqueName();
            $form->multipleFile('user_video', '训练视频')->saving(function ($paths) {
                // 可以转化为由 , 隔开的字符串格式
                // return implode(',', $paths);
                // 也可以转化为json
                return json_encode($paths);
            })->autoUpload()->uniqueName();

            $form->checkbox('position', '球队位置')
                ->options([
                    'PF' => 'PF',
                    'PG' => 'PG',
                    'SG' => 'SG',
                    'SF' => 'SF',
                    'C' => 'C'
                ])
                ->saving(function ($value) {
                    // 转化成json字符串保存到数据库
                    return json_encode($value);
                });
        });
    }
    public function change(Request $request){
        $id = $request['id'];
        $team_id = User::where('id',$id)->value('team_id');
        if ($team_id == 0){
            return response()->json([
                'status'  => true,
                'message' => '没有加入球队,无需解绑',
            ]);
        }
        $res = User::where('id',$id)->update(['team_id'=>0]);

        if ($res){
            return response()->json([
                'status'  => true,
                'message' => '解绑成功',
            ]);
        }else{
            return response()->json([
                'status'  => true,
                'message' => '解绑失败',
            ]);
        }

    }

}
